package com.haiqi.modules.map.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.WatersInfo;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.map.service.WatersInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HaiQi on 2017-2-20.
 */
@Controller
@RequestMapping("/manage/waters/info")
@SafeAuthority(menuCode = "monitorLocation")
public class WatersInfoController extends BaseController {
    @Autowired
    private WatersInfoService watersInfoService;

    @ResponseBody
    @RequestMapping("/ajax/list")
    public DataGridBean<WatersInfo> ajaxList(String type,PageBean pageBean){

        PageUtils.generatePage(pageBean);
        List<WatersInfo> list = null;

        if (StringUtils.isEmpty(type)){
            list = watersInfoService.getAllWatersInfo();
        }else if(type.equals("0")){
            list = watersInfoService.getAllWatersInfo();
        }else{
            list = watersInfoService.getAllByType(type);
        }

        PageInfo<WatersInfo> pageInfo = new PageInfo<>(list);
        DataGridBean<WatersInfo> gridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo,gridBean);
        return gridBean;
    }
    @RequestMapping("/delWaters")
    public String delWatersInfo(String id){
        boolean success = watersInfoService.delete(id);
        Map<String,Boolean> response = new HashMap<>();
        response.put("success",success);
        //return response;
        return "/modules/map/window/watersManager";
    }

    @RequestMapping("/updateState")
    @ResponseBody
    public Map changeWatersInfo(String reserve2,String id){
        if(reserve2.equals("1")){
            reserve2="2";
        }else if (reserve2.equals("2")){
            reserve2="1";
        }else{
            reserve2="1";
        }
        boolean success=watersInfoService.updateState(reserve2, id);
        Map<String,Boolean> response = new HashMap<>();
        response.put("success",success);
        return  response;
        //return "/modules/map/window/watersManager";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map addWatersInfo(WatersInfo watersInfo, HttpSession session){
        Map<String,Boolean> response = new HashMap<>();
        ResponseBean.UserBean user = getUser(session);
        if(org.apache.commons.lang.StringUtils.isNotEmpty(user.getName())){
            watersInfo.setReserve1(user.getName());
        }else{
            watersInfo.setReserve1(user.getLoginname());
        }
        if (watersInfo.getLongitude()!=null&&watersInfo.getLatitude()!=null){
            if (watersInfo.getLongitude().length==watersInfo.getLatitude().length){
                String temp="";
                for (int i=0;i<watersInfo.getLongitude().length;i++){
                   temp+=watersInfo.getLongitude()[i]+" "+watersInfo.getLatitude()[i]+",";
                }

                temp+=watersInfo.getLongitude()[0]+" "+watersInfo.getLatitude()[0];
                temp="("+temp+")";
                watersInfo.setCoordinate("POLYGON("+temp+")");
            }
        }

        boolean success = watersInfoService.save(watersInfo);
        response.put("success",success);
        //return "/modules/map/window/watersManager";
        return  response;
    }
    @RequestMapping("/getWkt")
    @ResponseBody
    public WatersInfo getOneById(String id){

        return watersInfoService.getOneWatersInfo(id);
    }
}
